import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/utils/TokenManageer.dart';
import 'package:flutter_enjoy_plus/utils/toast.dart';

class TanchuangDemoPage extends StatefulWidget {
  const TanchuangDemoPage({Key? key}) : super(key: key);

  @override
  State<TanchuangDemoPage> createState() => _TanchuangDemoPageState();
}

class _TanchuangDemoPageState extends State<TanchuangDemoPage> {
  Timer? timer;
  int countDown = 60;

  void _showAlertDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('AlertDialog'),
        content: const Text('这是一个普通弹窗'),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(),
            child: const Text('确定'),
          ),
        ],
      ),
    );
  }

  void _showSimpleDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => SimpleDialog(
        title: const Text('SimpleDialog'),
        children: [
          SimpleDialogOption(
            child: const Text('选项一'),
            onPressed: () => Navigator.of(context).pop(),
          ),
          SimpleDialogOption(
            child: const Text('选项二'),
            onPressed: () => Navigator.of(context).pop(),
          ),
        ],
      ),
    );
  }

  void _showBottomSheet(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (context) => Container(
        height: 200,
        alignment: Alignment.center,
        child: const Text('这是一个底部弹窗'),
      ),
    );
  }

  void _showSnackBar(BuildContext context) {
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(content: Text('这是一个SnackBar提示')),
    );
  }

  void _showCustomDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => Dialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
        child: Container(
          padding: const EdgeInsets.all(20),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Text('自定义弹窗', style: TextStyle(fontSize: 18)),
              const SizedBox(height: 16),
              const Text('这里可以放任何自定义内容'),
              const SizedBox(height: 16),
              ElevatedButton(
                onPressed: () => Navigator.of(context).pop(),
                child: const Text('关闭'),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _showDatePicker(BuildContext context) async {
    DateTime? picked = await showDatePicker(
      context: context,
      initialDate: DateTime.now(),
      firstDate: DateTime(2000),
      lastDate: DateTime(2100),
    );
    if (picked != null) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
            content:
                Text('选择的日期: ${picked.year}-${picked.month}-${picked.day}')),
      );
    }
  }

  void _showTimePicker(BuildContext context) async {
    TimeOfDay? picked = await showTimePicker(
      context: context,
      initialTime: TimeOfDay.now(),
    );
    if (picked != null) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('选择的时间: ${picked.format(context)}')),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('弹窗效果演示')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Row(
              children: [
                TextButton(
                  onPressed: () {
                    tokenManager.setToken('123456789012138888888','abcdefghjasdhkadhkawjs12138');
                  },
                  child: const Text('持久化存储-存'),
                ),
                TextButton(
                  onPressed: () async {
                    final token = await tokenManager.getToken();
                    PromptAction.showSuccess(token);
                  },
                  child: const Text('持久化存储-取'),
                ),
                TextButton(
                  onPressed: () {
                    tokenManager.deleteToken();
                  },
                  child: const Text('持久化存储-删'),
                ),
              ],
            ),
            Row(
              children: [
                TextButton(
                  onPressed: () {
                    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
                      setState(() {
                        countDown--;
                        if (countDown <= 0) {
                          timer.cancel();
                          countDown = 60;
                        }
                      });
                    });
                  },
                  child: const Text('开启定时器'),
                ),
                Text('秒数:$countDown'),
                TextButton(
                  onPressed: () {
                    timer?.cancel();
                  },
                  child: const Text('停止定时器'),
                ),
              ],
            ),
            Row(
              children: [
                TextButton(
                    onPressed: () {
                      Navigator.pushNamed(context, '/profile');
                    },
                    child: Text('个人信息')),
                TextButton(
                    onPressed: () {
                      Navigator.pushNamed(context, '/login');
                    },
                    child: Text('登录')),
              ],
            ),
            ElevatedButton(
              onPressed: () => _showAlertDialog(context),
              child: const Text('AlertDialog 普通弹窗'),
            ),
            const SizedBox(height: 12),
            ElevatedButton(
              onPressed: () => _showSimpleDialog(context),
              child: const Text('SimpleDialog 简单弹窗'),
            ),
            const SizedBox(height: 12),
            ElevatedButton(
              onPressed: () => _showBottomSheet(context),
              child: const Text('BottomSheet 底部弹窗'),
            ),
            const SizedBox(height: 12),
            ElevatedButton(
              onPressed: () => _showSnackBar(context),
              child: const Text('SnackBar 底部提示'),
            ),
            const SizedBox(height: 12),
            ElevatedButton(
              onPressed: () => _showCustomDialog(context),
              child: const Text('自定义弹窗'),
            ),
            const SizedBox(height: 12),
            ElevatedButton(
              onPressed: () => _showDatePicker(context),
              child: const Text('日期选择弹窗'),
            ),
            const SizedBox(height: 12),
            ElevatedButton(
              onPressed: () => _showTimePicker(context),
              child: const Text('时间选择弹窗'),
            ),
          ],
        ),
      ),
    );
  }
}
